package cn.witsky.rtp2ws.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.witsky.rtp2ws.Server;

public class FileUtil {
	private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
	private static String tarCommand = "tar -zcvf %s/%s %s/%s";
	private static String unTarCommand = "tar -zxvf %s/%s  -C %s/  --strip-components 1";	
	private static String clearCommand="find %s -mtime +%s -name '*' -exec rm -Rf {} \\;";	
	private static String copyCommand = "cp %s/%s %s";	
	
	public static String getFileExtension(String fileName) {
		if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
			return fileName.substring(fileName.lastIndexOf(".") + 1);
		} else {
			return "";
		}
	}
	
	public static List<String> toArrayByFileReader() {
		File file = new File(Server.retryLog);
		List<String> array = new ArrayList<>();
		if (file.exists()) {
			try {
				FileReader fr = new FileReader(Server.retryLog);
				BufferedReader bf = new BufferedReader(fr);
				String str;
				// 按行读取字符串
				while ((str = bf.readLine()) != null) {
					array.add(str.trim());
				}
				bf.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			(new File(Server.retryLog)).delete();
		}
		return array;
	}

	public static boolean tar(String src, String fileName,String dst,String tarFileName) {
		boolean isSuc = false;
		int exitValue = -1;
		Process process = null;
		try {
			String command = String.format(tarCommand, dst, tarFileName,src,fileName);
			process = Runtime.getRuntime().exec(command);
			exitValue = process.waitFor();
			if (exitValue != 0) {
				logger.warn("执行shell命令{}失败,result:{}",command,exitValue);
			}else {
				logger.info("执行shell命令{}成功",command);
				isSuc=true;
			}
		} catch (Exception ex) {
			logger.info("执行tar打包命令出现异常:",ex);
		}
		return isSuc;
	}
	
	public static boolean unTar(String src, String fileName, String dst) {
		boolean isSuc = false;
		int exitValue = -1;
		Process process = null;
		try {
			String command = String.format(unTarCommand, src, fileName, dst, fileName);
			process = Runtime.getRuntime().exec(command);
			exitValue = process.waitFor();
			if (exitValue != 0) {
				logger.warn("执行shell命令{}失败,result:{}",command,exitValue);
			}else {
				logger.warn("执行shell命令{}成功",command);
				isSuc=true;
			}
		} catch (Exception ex) {
			logger.info("执行tar解压命令出现异常:",ex);
		}
		return isSuc;
	}
	public static boolean clearFile(String src,int days) {
		boolean isSuc = false;
		int exitValue = -1;
		Process process = null;
		try {
			String command = String.format(clearCommand, src, days);
			process = Runtime.getRuntime().exec(command);
			exitValue = process.waitFor();
			if (exitValue != 0) {
				logger.warn("执行shell命令{}失败,result:{}",command,exitValue);
			}else {
				logger.warn("执行shell命令{}成功",command);
				isSuc=true;
			}
		} catch (Exception ex) {
			logger.info("执行清理文件命令出现异常:",ex);
		}
		return isSuc;
	}
	
	public static boolean copy(String src, String fileName, String dst) {
		boolean isSuc = false;
		int exitValue = -1;
		Process process = null;
		try {
			String command = String.format(copyCommand, src, fileName, dst);
			process = Runtime.getRuntime().exec(command);
			exitValue = process.waitFor();
			if (exitValue != 0) {
				logger.warn("执行shell命令{}失败,result:{}",command,exitValue);
			}else {
				logger.warn("执行shell命令{}成功",command);
				isSuc=true;
			}
		} catch (Exception ex) {
			logger.info("执行cp拷贝命令出现异常:",ex);
		}
		return isSuc;
	}
}
